package com.example.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.InFormDetailMapper;
import com.example.mapper.InFormMapper;
import com.example.mapper.WarehouseMapper;
import com.example.model.dtos.PageResult;
import com.example.model.pojo.InForm;
import com.example.model.pojo.InFormDetail;
import com.example.model.pojo.Warehouse;
import com.example.service.InFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class InFormServiceImpl implements InFormService {

    @Autowired(required = false)
    InFormMapper inFormMapper;

    @Autowired(required = false)
    InFormDetailMapper inFormDetailMapper;

    @Autowired(required = false)
    WarehouseMapper warehouseMapper;

    @Override
    public Boolean submit(InForm inForm, List<InFormDetail> inFormDetailList) {
        int insert = inFormMapper.insert(inForm);
        String myId = inForm.getMyId();
        for (InFormDetail inFormDetail : inFormDetailList) {
            inFormDetail.setMyId(null);
            inFormDetail.setInFormId(myId);
            inFormDetailMapper.insert(inFormDetail);
        }

        return insert!=0;
    }

    @Override
    public PageResult getUnreviewedPage(Integer pageStart, Integer pageSize, String inFormId, String startDate, String endDate) {
        //创建分页对象并将当前页码和页面大小写入
        Page<InForm> inFormPage =new Page<>(pageStart, pageSize);
        QueryWrapper<InForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status_", 0);

        if (StringUtils.isBlank(startDate)){
            startDate="1970-01-01";
        }
        if (StringUtils.isBlank(endDate)){
            endDate="2099-01-01";
        }
        DateTime start = DateUtil.parse(startDate);
        DateTime end = DateUtil.parse(endDate);

        queryWrapper.between("date_",start,end);
        if (StringUtils.isNotBlank(inFormId)){
            queryWrapper.like("my_id_", inFormId);
        }

        inFormMapper.selectPage(inFormPage, queryWrapper);
        List<InForm> records = inFormPage.getRecords();

//        返回一个分页后的结果
        PageResult pageResult = new PageResult(pageStart.longValue(), pageSize.longValue(), inFormPage.getTotal(),records);

        return pageResult;
    }

    @Override
    public List<InFormDetail> details(String myId) {
        QueryWrapper<InFormDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("in_form_id_", myId);
        List<InFormDetail> details = inFormDetailMapper.selectList(queryWrapper);

        return details;
    }

    @Override
    public Boolean audit(InForm inForm) {
        //更新订单状态
        UpdateWrapper<InForm> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("my_id_", inForm.getMyId());
        updateWrapper.set( "status_", inForm.getStatus());
        int insert = inFormMapper.update(null, updateWrapper);

        if (inForm.getStatus().equals("1")){
//        查询订单内容，获得采购详情
        QueryWrapper<InFormDetail> inFormDetailQueryWrapper = new QueryWrapper<>();
        inFormDetailQueryWrapper.eq("in_form_id_", inForm.getMyId());
        List<InFormDetail> inFormDetailList = inFormDetailMapper.selectList(inFormDetailQueryWrapper);
//        根据采购详情完成采购，更新仓库
            for (InFormDetail inFormDetail : inFormDetailList) {
//                根据物资ID查询库存
                QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper<>();
                warehouseQueryWrapper.eq("materials_id_", inFormDetail.getMaterialsId());
                Warehouse warehouse = warehouseMapper.selectOne(warehouseQueryWrapper);
//                若没有则在仓库中新增该物资
                if (warehouse ==null){
                    Warehouse newWarehouse = new Warehouse(null, inFormDetail.getMaterialsId(), inFormDetail.getMaterialsName(), inFormDetail.getAmount());
                    warehouseMapper.insert(newWarehouse);
                }else {
                    Integer total = warehouse.getTotal();
                    Integer amount = inFormDetail.getAmount();
                    warehouse.setTotal(total+amount);
                    warehouseMapper.updateById(warehouse);
                }
            }
        }

        return insert!=0;
    }

    @Override
    public PageResult getAllInForm(Integer pageStart, Integer pageSize, String inFormId, String startDate, String endDate) {
        //创建分页对象并将当前页码和页面大小写入
        Page<InForm> inFormPage =new Page<>(pageStart, pageSize);
        QueryWrapper<InForm> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isBlank(startDate)){
            startDate="1970-01-01";
        }
        if (StringUtils.isBlank(endDate)){
            endDate="2099-01-01";
        }
        DateTime start = DateUtil.parse(startDate);
        DateTime end = DateUtil.parse(endDate);

        queryWrapper.between("date_",start,end);
        if (StringUtils.isNotBlank(inFormId)){
            queryWrapper.like("my_id_", inFormId);
        }

        inFormMapper.selectPage(inFormPage, queryWrapper);
        List<InForm> records = inFormPage.getRecords();

//        返回一个分页后的结果
        PageResult pageResult = new PageResult(pageStart.longValue(), pageSize.longValue(), inFormPage.getTotal(),records);

        return pageResult;
    }
}
